package com.crm.marketing.controller;

import com.crm.common.pojo.Chance;
import com.crm.global.exception.ExceptionEnum;
import com.crm.global.result.Message;
import com.crm.marketing.service.ChanceService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/chance")
@Api(tags = "销售机会管理")
public class ChanceController {
    @Autowired
    private ChanceService chanceService;

    @PostMapping("/findAll")
    @ApiOperation(value = "查询所有", notes = "根据条件(客户名称,联系人)查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true),
            @ApiImplicitParam(name = "size", value = "页面大小",required = true)
    })
    public ResponseEntity<Message<PageInfo<Chance>>> findAll(int page, int size, @RequestBody(required = false) Chance chance) {
        return ResponseEntity.ok(new Message<PageInfo<Chance>>(ExceptionEnum.SUCCESS,chanceService.findAll(page, size, chance)));
    }

    @GetMapping("/delete")
    @ApiOperation(value = "根据id删除")
    @ApiImplicitParam(name = "id",value = "机会id",required = true)
    public ResponseEntity<Message<String>> delete(long id) {
        chanceService.deleteById(id);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }

    @GetMapping("/findbyId")
    @ApiOperation(value = "根据id查找")
    @ApiImplicitParam(name = "id",value = "机会id",required = true)
    public ResponseEntity<Message<Chance>> findById(long id) {
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS,chanceService.findById(id)));
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增机会")
    public ResponseEntity<Message<String>> save(@RequestBody(required = true) Chance chance) {
        chanceService.saveOrUpdate(chance);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改机会")
    public ResponseEntity<Message<String>> update(@RequestBody(required = true) Chance chance) {
        chanceService.saveOrUpdate(chance);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }

    @PostMapping("/assign")
    @ApiOperation(value = "指派")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "机会id",required = true),
            @ApiImplicitParam(name = "name",value = "被分配的人",required = true)
    })
    public ResponseEntity<Message<String>> assign(long id,String name) {
        Chance chance = chanceService.findById(id);
        chance.setDueTo(name);
        chanceService.saveOrUpdate(chance);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }

}
